package cloudspace.util.jassistwrappers;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;


/**
 * The Class VMURI. These are javassist wrapper function for use when localizing
 * and globalizing URI's
 */
public class VMURI
{

    /**
     * Globalize a uri.
     * 
     * @param uri
     *            the uri to be globalized
     * 
     * @return the new global uri (System view)
     */
    public static URI globalizeURI( URI uri )
    {
        if ( uri == null )
        {
            return uri;
        }
        try
        {
            if ( uri.getScheme().equals( "file" ) )
            {
                String scheme = uri.getScheme();
                String host = uri.getHost();
                String frag = uri.getFragment();
                String path = uri.getPath();
                path = VMFile.toGlobalName( path );
                path = path.replace( '\\', '/' );
                if ( File.separatorChar == '\\' )
                {
                    path = '/' + path;
                }
                return new URI( scheme, host, path, frag );
            }
            return uri;
        }
        catch ( URISyntaxException e )
        {
            return uri;
        }

    }


    /**
     * Localizes a URI to the user view of it.
     * 
     * @param uri
     *            the to localize
     * 
     * @return the URI (User View)
     */
    public static URI localizeFileURI( File myFile, URI uri )
    {
        try
        {
            String scheme = uri.getScheme();
            String host = uri.getHost();
            String frag = uri.getFragment();
            String path = myFile.getPath();
            path = VMFile.toLocalAbsName( myFile, path );
            return new URI( scheme, host, path, frag );
        }
        catch ( URISyntaxException e )
        {
            return uri;
        }
    }
}
